package com.ktwlsoft.archivemanage.controller;

import com.ktwlsoft.archivemanage.domain.business.businessService.QuestionService;
import com.ktwlsoft.archivemanage.payload.Result;
import com.ktwlsoft.archivemanage.payload.ResultType;
import com.ktwlsoft.archivemanage.payload.question.QuestionPageRequest;
import com.ktwlsoft.archivemanage.payload.question.QuestionUpOrDownRequest;
import com.ktwlsoft.archivemanage.payload.question.QuestionSaveRequest;
import com.ktwlsoft.archivemanage.payload.question.QuestionUpdateRequest;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.io.UnsupportedEncodingException;

/**
 * @author JokerMo
 * @version 1.0
 * @date 2019/12/12 15:18
 */
@Api(value = "问题维护", description = "问题维护")
@RestController
@RequestMapping("/question")
public class QuestionController {
    /**
     * 问题管理
     */
    private final QuestionService questionService;

    public QuestionController(QuestionService questionService) {
        this.questionService = questionService;
    }

    /**
     * 问题分页
     *
     * @return ResponseEntity<?>
     */
    @ApiOperation(value = "问题分页")
    @GetMapping("/list")
    public ResponseEntity<?> list(QuestionPageRequest request) {
        return ResponseEntity.ok(questionService.findPage(request));
    }

    /**
     * 问题详情
     *
     * @return ResponseEntity<?>
     */
    @ApiOperation(value = "问题详情", httpMethod = "GET", produces = "application/json", response = ResponseEntity.class)
    @ResponseBody
    @GetMapping("/getById")
    public ResponseEntity<?> getById(@RequestParam Long id) throws UnsupportedEncodingException {
        return ResponseEntity.ok(questionService.getById(id));
    }

    /**
     * 问题新增
     *
     * @param request
     * @param bindingResult
     * @return ResponseEntity<?>
     */
    @ApiOperation(value = "问题新增", httpMethod = "POST", produces = "application/json", response = ResponseEntity.class)
    @ResponseBody
    @PostMapping("/add")
    public ResponseEntity<?> add(@Valid @RequestBody QuestionSaveRequest request) throws UnsupportedEncodingException {
        Result result = questionService.save(request);
        return ResponseEntity.ok(result);
    }

    /**
     * 问题修改
     *
     * @param request
     * @param bindingResult
     * @return ResponseEntity<?>
     */
    @ApiOperation(value = "问题修改", httpMethod = "POST", produces = "application/json", response = ResponseEntity.class)
    @ResponseBody
    @PostMapping("/edit")
    public ResponseEntity<?> edit(@Valid @RequestBody QuestionUpdateRequest request) {
        Result result = questionService.edit(request);
        return ResponseEntity.ok(result);
    }

    /**
     * 问题删除
     *
     * @param id id
     * @return ResponseEntity<?>
     */
    @ApiOperation(value = "问题删除", httpMethod = "GET", produces = "application/json", response = ResponseEntity.class)
    @ResponseBody
    @GetMapping("/delete")
    public ResponseEntity<?> delete(@ApiParam(name = "id", value = "问题id") @RequestParam Long id) {
        Result result = questionService.deleteById(id);
        return ResponseEntity.ok(result);
    }

    @ApiOperation(value = "问题上下架", httpMethod = "POST", produces = "application/json", response = ResponseEntity.class)
    @PostMapping("/upOrDown")
    public ResponseEntity<?> upOrDown(@RequestBody QuestionUpOrDownRequest request) {
        return ResponseEntity.ok(Result.ok(questionService.upOrDownById(request.getId(), request.getState())));
    }
}
